$(document).ready(function(){
    //If we are creating a meal
    if ($('#meal_list .listboxItem').size() == 0){
        $('#bottom_table_div').hide();
    }
    
    $("#orders_allowed").checkbox({
        empty: getBaseURL()+"js/plugins/checkbox/empty.png"
    });
    
    $("#search_term").keyup(function(){
        doSearch();
    });
    $("#search_cat").change(function(){
        doSearch();
    });
    
    $("#reset_filters").click(function(){
        $("#search_term").val("");
        $("#search_cat").val(-1);
        $("#search_term").keyup();
    });
        
    doSearch();
    
    registerButtons();
    
    $('#bottom_table select').change(changeMade);
    $('#orders_allowed').change(changeMade);
    
    $('#discard_changes').click(function(){
        window.location = getBaseURL()+'meal_allocation/meal_allocation';
    });
    
    $("#save_changes").click(function(){
        var meals = [];
        $("#meal_list .listboxItem").each(function(){
            meals.push($(this).attr("value")); // option id value
        });
        
        
        $.post(getBaseURL()+"meal_allocation/meal_allocation/saveMeals/", 
            {
                meals: meals,
                orderable: boolToInt($("#orders_allowed").attr("Checked")),
                servetime: $("#serving_time").val(),
                cutoff: $("#cutoff_time").val(),
                mealtime: $("#mealtime_id").val()
            },
            function(data){
                if (data != "good"){
                    alert(data);
                }
                else{
                    window.onbeforeunload = null;
                    window.location = document.referrer;//getBaseURL()+"meal_allocation/meal_allocation/month";
                }
            });
    });
    
    $("#add_meal").click(function(){
        if ($("#search_results").val() != ""){
            
            $('#bottom_table_div').slideDown(700);
            
            changeMade();
            var exists = false;
            $("#meal_list .listboxItem").each(function(){
                if ($(this).attr('value') == $("#search_results").val()){
                    exists = true;
                    return false;
                }
            });
            if (!exists){
                $("#meal_list").append(createListboxItem(
                    $("#search_results").val(), $("#search_results").attr('title')
                ));
            }
            
            registerButtons();
        }
    });
});


function doSearch(){
    $("#search_results").html("Loading...").attr("value","");
    $("#add_meal").removeClass("button");
    $("#add_meal").addClass("buttonLight");
    
    var search_term = $("#search_term").val();
    var search_cat = $("#search_cat").val();

    $.post(getBaseURL()+"meal_allocation/meal_allocation/searchResults/", 
        {
            search_term: search_term,
            category: search_cat
        },
        function(data){
            $("#search_results").html(data);
        }
    ); 
}

function selectMeal(elem,id){
    $('#search_results .listItem').removeClass('selected');
    
    $("#add_meal").removeClass("buttonLight");
    $("#add_meal").addClass("button");
    
    $(elem).addClass('selected');
    $(elem).parents("#search_results").attr("value",id);
    $(elem).parents("#search_results").attr("title",$(elem).find('.listItemTitle').text());
}

function registerButtons(){
    $('#meal_list .listboxItem .listboxItemClose').mouseup(function(){
        changeMade();
        if ($('#meal_list .listboxItem').size() == 1){
            $('#bottom_table_div').slideUp(700);
        }
    });
}